package cn.lzit.request.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResultServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username"); // 获取用户姓名
        String pwd = request.getParameter("password"); // 获取用户密码
        String age = request.getParameter("age"); // 获取用户年龄
        String sex = request.getParameter("sex"); // 获取用户性别
        String[] loves = request.getParameterValues("loves"); // 获取用户爱好
        String email = request.getParameter("email"); // 获取用户EAMIL
        String tel = request.getParameter("tel"); // 获取用户联系电话

        // 中文乱码处理，取决于你的tomcat版本，如果版本高于6.5是不需要的
        /*username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
        if (loves != null) {
            for (int i = 0; i < loves.length; i++)
                loves[i] = new String(loves[i].getBytes("ISO-8859-1"), "UTF-8");
        }*/
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pWriter = response.getWriter();
        pWriter.print("<body bgcolor='pink'>");
        if (pwd.length() < 6 || pwd.length() > 10) {
            response.sendRedirect("register.html"); // 重定向到注册页面
            return;
        }
        // 显示用户的注册信息
        pWriter.print("用户名: " + username + "<br/>");
        pWriter.print("密码:" + pwd + "<br/>");
        pWriter.print("年龄:" + age + "<br/>");
        pWriter.print("性别:" + sex + "<br/>");
        pWriter.print("爱好: " + Arrays.toString(loves) + "<br/>");
        pWriter.print("Email:" + email + "<br/>");
        pWriter.print("Tel:" + tel + "<br/>");
        pWriter.print("</body>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}